package com.wpen.unit.core.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpen.unit.core.service.NoticeHelper;
import com.wpen.unit.enums.ExecStatus;
import com.wpen.unit.enums.NoticeLevel;
import com.wpen.unit.infra.mybatis.job.po.BatchExecLogPo;
import com.wpen.unit.infra.mybatis.job.po.JobExec1LogPo;
import com.wpen.unit.infra.mybatis.job.po.TaskExecLogPo;
import com.wpen.unit.infra.mybatis.system.po.NoticePo;
import com.wpen.unit.infra.repository.system.NoticeRepository;
import com.wpen.unit.utils.DateUtil;
import com.wpen.unit.utils.IdUtil;

@Service
public class NoticeHelperImpl implements NoticeHelper {
	@Autowired
	private NoticeRepository noticeRepository;

	@Override
	public void recordNotice(BatchExecLogPo batchExecLogPo) {
		String batchCode = batchExecLogPo.getBatchCode();
		String date = batchExecLogPo.getExecDate();
		long batchTrggerTime = batchExecLogPo.getBatchTriggerTime();
		NoticePo noticePo = new NoticePo();
		noticePo.setId(IdUtil.nextId());
		noticePo.setCreateTime(new Date());
		NoticeLevel noticeLevel = null;
		if (ExecStatus.Finished.value().equals(batchExecLogPo.getExecStatus())) {
			noticeLevel = NoticeLevel.Emerg;
		} else {
			noticeLevel = NoticeLevel.Normal;
		}
		noticePo.setPriorityLevel(noticeLevel.value());
		noticePo.setNoticeTitle("批次[ " + batchCode + " / " + date + "]执行完成。");
		noticePo.setNoticeName(
				"【" + noticeLevel.desc() + "】批次[ " + batchCode + " / " + date + " / " + batchTrggerTime + "]执行完成。");
		noticePo.setNoticeContext("批次[ " + batchCode + " / " + date + " / " + batchTrggerTime + "]执行完成。开始时间："
				+ DateUtil.format(batchExecLogPo.getStartupTime(), DateUtil.FORMAT_DATETIME) + "，结束时间："
				+ DateUtil.format(batchExecLogPo.getEndTime(), DateUtil.FORMAT_DATETIME));

		noticeRepository.insert(noticePo);
	}

	@Override
	public void recordNotice(TaskExecLogPo taskExecLogPo) {
		String taskCode = taskExecLogPo.getTaskCode();
		String date = taskExecLogPo.getExecDate();
		long batchTrggerTime = taskExecLogPo.getBatchTriggerTime();
		NoticePo noticePo = new NoticePo();
		noticePo.setId(IdUtil.nextId());
		noticePo.setCreateTime(new Date());
		NoticeLevel noticeLevel = null;
		if (ExecStatus.Finished.value().equals(taskExecLogPo.getExecStatus())) {
			noticeLevel = NoticeLevel.Emerg;
		} else {
			noticeLevel = NoticeLevel.Normal;
		}
		noticePo.setPriorityLevel(noticeLevel.value());
		noticePo.setNoticeTitle("任务[ " + taskCode + " / " + date + "]执行完成。");
		noticePo.setNoticeName(
				"【" + noticeLevel.desc() + "】任务[ " + taskCode + " / " + date + " / " + batchTrggerTime + "]执行完成。");
		noticePo.setNoticeContext("任务[ " + taskCode + " / " + date + " / " + batchTrggerTime + "]执行完成。开始时间："
				+ DateUtil.format(taskExecLogPo.getStartupTime(), DateUtil.FORMAT_DATETIME) + "，结束时间："
				+ DateUtil.format(taskExecLogPo.getEndTime(), DateUtil.FORMAT_DATETIME));

		noticeRepository.insert(noticePo);
	}

	@Override
	public void recordNotice(JobExec1LogPo jobExec1LogPo) {
		if (!ExecStatus.Excption.value().equals(jobExec1LogPo.getExecStatus())) {
			return;
		}
		String batchCode = jobExec1LogPo.getJobCode();
		String date = jobExec1LogPo.getExecDate();
		long batchTrggerTime = jobExec1LogPo.getBatchTriggerTime();
		NoticePo noticePo = new NoticePo();
		noticePo.setId(IdUtil.nextId());
		noticePo.setCreateTime(new Date());
		NoticeLevel noticeLevel = null;
		noticeLevel = NoticeLevel.Emerg;
		noticePo.setPriorityLevel(noticeLevel.value());

		noticePo.setNoticeTitle("作业[ " + batchCode + " / " + date + "]执行异常。");
		noticePo.setNoticeName("【" + noticeLevel.desc() + "】任务[ " + batchCode + " / " + batchTrggerTime + "]执行异常。");
		noticePo.setNoticeContext("任务[ " + batchCode + " / " + date + " / " + batchTrggerTime + "]执行异常。开始时间："
				+ DateUtil.format(jobExec1LogPo.getStartupTime(), DateUtil.FORMAT_DATETIME) + "，结束时间："
				+ DateUtil.format(jobExec1LogPo.getEndTime(), DateUtil.FORMAT_DATETIME));

		noticeRepository.insert(noticePo);
	}

}
